<?
/*

Copyright 2009 Matthew C. Dettinger

This program is free software: you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

function c_establish_db_conn($config) {
	$link = mysqli_connect($config[db][host], $config[db][username], $config[db][password], $config[db][dbname]);
	/* check connection */
	if (!$link) {
	    printf("Connect failed: %s\n", mysqli_connect_error());
	    exit();
	}
	
	return $link;
}

function c_generate_uuid() {
	// generates a random uuid
	$uuid = sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
	mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
	mt_rand( 0, 0x0fff ) | 0x4000,
	mt_rand( 0, 0x3fff ) | 0x8000,
	mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
	$uuid = strtoupper($uuid);
	return $uuid;
}

function c_redirect() {
	header('Location: index.php');
}

function c_announce_user_error($msg) {
	echo '<div id="error">';
	echo '<h3>'.$msg.'</h3>';
	echo '</div>';
	die();
}

function c_announce_system_message($msg) {
	echo '<div id="system_message">';
	echo '<h3>'.$msg.'</h3>';
	echo '</div>';
	die();
}

?>